﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HotelMatchUtility.Utility
{
    public class CommonUtility
    {
        #region COMPARE_DATA
        public static Dictionary<string, string> ADDRESS_REPLACE_DIC = new Dictionary<string, string>
        {
            {"ln","lane"}
           ,{"dr","driver"}
           ,{"ave","avenue"}
           ,{"blvd","boulevard"}
           ,{"rd","road"}
           ,{"st","street"}
           ,{"n","north"}
           ,{"s","south"}
           ,{"w","west"}
           ,{"e","east"}
           ,{"nw","northwest"}
           ,{"ne","northeast"}
           ,{"sw","southwest"}
           ,{"se","southeast"}
           ,{"first","1st"}
           ,{"second","2nd"}
           ,{"third","3rd"}
           ,{"fourth","4th"}
           ,{"fifth","5th"}
           ,{"sixth","6th"}
           ,{"seventh","7th"}
           ,{"eighth","8th"}
           ,{"ninth","9th"}
           ,{"tenth","10th"}
           ,{"eleventh","11th"}
           ,{"twelfth","12th"}
        };

        public static char[] SIGN = { ',', ' ', '.', '-' };

        public static List<string> IGNORE_WORDS = new List<string> { "marriott", "hilton", "hotel", ""};
        #endregion

        public static string FormatTelephone(string telephone)
        {
            telephone = telephone.Trim();
            telephone = telephone.Replace(" ", "");
            telephone = telephone.Replace("+", "");
            telephone = telephone.Replace("-", "");
            telephone = telephone.Replace("(0)", "");

            long intTelephone = 0;
            bool result = long.TryParse(telephone, out intTelephone);
            if (result)
            {
                telephone = intTelephone.ToString();
            }
            else
            {
                telephone = "";
            }

            return telephone;
        }

        public static string DConvertAllSpecialCharactor(string pValue)
        {
            string result = pValue;
            result = result.Replace("&#192;", "a");
            result = result.Replace("&#193;", "a");
            result = result.Replace("&#194;", "a");
            result = result.Replace("&#195;", "a");
            result = result.Replace("&#196;", "a");
            result = result.Replace("&#197;", "a");
            result = result.Replace("&#199;", "c");
            result = result.Replace("&#200;", "e");
            result = result.Replace("&#201;", "e");
            result = result.Replace("&#202;", "e");
            result = result.Replace("&#203;", "e");
            result = result.Replace("&#204;", "i");
            result = result.Replace("&#205;", "i");
            result = result.Replace("&#206;", "i");
            result = result.Replace("&#207;", "i");
            result = result.Replace("&#208;", "d");
            result = result.Replace("&#209;", "n");
            result = result.Replace("&#210;", "o");
            result = result.Replace("&#211;", "o");
            result = result.Replace("&#212;", "o");
            result = result.Replace("&#213;", "o");
            result = result.Replace("&#214;", "o");
            result = result.Replace("&#217;", "u");
            result = result.Replace("&#218;", "u");
            result = result.Replace("&#219;", "u");
            result = result.Replace("&#220;", "u");
            result = result.Replace("&#221;", "y");
            result = result.Replace("&#224;", "a");
            result = result.Replace("&#225;", "a");
            result = result.Replace("&#226;", "a");
            result = result.Replace("&#227;", "a");
            result = result.Replace("&#228;", "a");
            result = result.Replace("&#229;", "a");

            result = result.Replace("&#231;", "c");
            result = result.Replace("&#232;", "e");
            result = result.Replace("&#233;", "e");
            result = result.Replace("&#234;", "e");
            result = result.Replace("&#235;", "e");
            result = result.Replace("&#236;", "i");
            result = result.Replace("&#237;", "i");
            result = result.Replace("&#238;", "i");
            result = result.Replace("&#239;", "i");
            result = result.Replace("&#240;", "o");
            result = result.Replace("&#241;", "n");
            result = result.Replace("&#242;", "o");
            result = result.Replace("&#243;", "o");
            result = result.Replace("&#244;", "o");
            result = result.Replace("&#245;", "o");
            result = result.Replace("&#246;", "o");

            result = result.Replace("&#249;", "u");
            result = result.Replace("&#250;", "u");
            result = result.Replace("&#251;", "u");
            result = result.Replace("&#252;", "u");
            result = result.Replace("&#253;", "y");

            result = result.Replace("&#255;", "y");

            result = result.Replace("á", "a");
            result = result.Replace("é", "e");
            result = result.Replace("í", "i");
            result = result.Replace("ó", "o");
            result = result.Replace("ú", "u");

            return result;
        }

        public static List<string> FormatNameOrAddress(string toFormat)
        {
            List<string> returnList = new List<string>();
            string[] resultArray = toFormat.Split(SIGN);

            foreach (var ignoreW in IGNORE_WORDS)
            {
                resultArray = resultArray.Where(p => p != ignoreW).ToArray();
            }
            string temp = "";
            foreach (var result in resultArray)
            {
                temp = result;
                foreach (var keypair in ADDRESS_REPLACE_DIC)
                {
                    if (keypair.Key == result.ToLower()) temp = keypair.Value;
                }
                returnList.Add(temp);
            }

            return returnList;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetList"></param>
        /// <param name="toCompareList"></param>
        /// <param name="accurency">需要达到的精确率</param>
        /// <returns></returns>
        public static bool CompareTwoStringSimilary(List<string> targetList, List<string> toCompareList
            , double accurency
            )
        {
            int compareCount = 0; //比较总次数
            int containCount = 0; //包含的次数
            double currentPercent = 0.0; //包含的比率
            foreach (string targetName in targetList)
            {
                if (string.IsNullOrEmpty(targetName.Trim())) continue;
                compareCount++;
                foreach (string toCompareName in toCompareList)
                {
                    if (targetName.ToUpper() == toCompareName.ToUpper())
                    {
                        containCount++;
                        break;
                    }
                }
            }
            if (compareCount == 0) return false;
            currentPercent = (double)containCount / compareCount;
            //Magic Number To Do
            if (compareCount <= 2 && toCompareList.Count <= 3)
                return currentPercent == 1;
            if (compareCount <= 2 && toCompareList.Count >= 8)
                return false;

            return currentPercent >= accurency;
        }
    }
}
